1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class MusicController : MonoBehaviour {
6     
public static MusicController instance;
7
8     
public AudioClip background;
9     
public AudioClip gameplay;
10     
public AudioClip success;
11     
public AudioClip failed;
12
13     
private AudioSource audioSource;
14
15     
void Awake(){
16         CreateInstance ();
17         audioSource = GetComponent<AudioSource> ();
18     }
19
20     
// Use this for initialization
21     
void Start () {
22         
23     }
24
25     
void CreateInstance(){
26         
if (instance != null) {
27             Destroy (gameObject);
28         }
else {
29             instance =
this;
30             DontDestroyOnLoad (gameObject);
31         }
32     }
33
34     
public void PlayBackgroundSound(){
35         
if(background){
36             audioSource.clip = background;
37             audioSource.loop =
true;
38             audioSource.Play ();
39         }
40     }
41
42     
public void StopAllSound(){
43         
if(audioSource.isPlaying){
44             audioSource.Stop ();
45         }
46     }
47
48     
public void PlayGameplaySound(){
49         
if(gameplay){
50             audioSource.clip = gameplay;
51             audioSource.loop =
true;
52             audioSource.Play ();
53         }
54     }
55
56     
public void PlaySuccessSound(){
57         
if(success){
58             audioSource.PlayOneShot (success);
59         }
60     }
61
62     
public void PlayFailedSound(){
63         
if(failed){
64             audioSource.PlayOneShot (failed);
65         }
66     }
67
68 }


Gõ tìm kiếm nhanh...